﻿/**
* @author Alexander Rindo
* @project Flashview
* @version 0.4
* @purpose Class for the ThumbnailScroller Movieclip
**/

package com.alexrindo.flashview.UI.thumbnails 
{
	import com.alexrindo.flashview.layout.LayoutManager;
	import com.greensock.TweenMax;
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	import flash.display.MovieClip
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class ThumbnailScroller extends MovieClip
	{
		private var _layout:LayoutManager
		
		private var _barWidthRatio:int
		private var _xOffset:int
		private var _xMin:int = 0
		private var _xMax:int
		private var _ratio:Number
		private var _timeScroll:Number = 0.5;
		private var _arrowStep:int = 40
		private var _scrollTo:Number;
		private var _contentWidth:int = 3000
		private var _thumbContent:Sprite
		
		public function ThumbnailScroller() 
		{
			init()
			addListeners()
		}
		
		private function init():void
		{
			visible = false
		}
		
		private function addListeners():void
		{
			trackerBar.addEventListener(MouseEvent.MOUSE_DOWN, ThumbScrollerDown)
			trackerBackground.addEventListener(MouseEvent.MOUSE_DOWN, TrackDown)
			parent.addEventListener(ThumbnailHolder.THUMBS_LOADED, onThumbsLoaded)
			rightScrollArrow.addEventListener(MouseEvent.CLICK, onRightArrowClick)
			leftScrollArrow.addEventListener(MouseEvent.CLICK, onLeftArrowClick)
		}
		
		private function onThumbsLoaded(e:Event):void
		{
			visible = true
			_thumbContent = parent.content
			_contentWidth = _thumbContent.width
		}
		
		public function resizeElements():void
		{
			rightScrollArrow.x = stage.stageWidth - leftScrollArrow.width - rightScrollArrow.width;
			trackerBackground.width = stage.stageWidth - leftScrollArrow.width - 15;
			
			_barWidthRatio = (trackerBackground.width) * (stage.stageWidth) / (_contentWidth);
			
			trackerBar.back_mc.x = trackerBar.left_mc.x + trackerBar.left_mc.width;
			trackerBar.back_mc.width = _barWidthRatio - 60;
			trackerBar.right_mc.x = trackerBar.back_mc.width + trackerBar.right_mc.width;
			
			_xMin = 0;
			_xMax = trackerBackground.width - trackerBar.width + 1
			
			trackerBar.x = checkPosition(trackerBar.x)
			
			parent.hideThumbHolder.x = rightScrollArrow.x - 40
		}
		
		private function onLeftArrowClick(e:MouseEvent):void 
		{
			trackerBar.x -= _arrowStep
			trackerBar.x = checkPosition(trackerBar.x)
			upDateThumbNailPosition()
		}
		
		private function onRightArrowClick(e:MouseEvent):void 
		{
			trackerBar.x += _arrowStep
			trackerBar.x = checkPosition(trackerBar.x)
			upDateThumbNailPosition()
		}
		
		private function ThumbScrollerDown(e:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_UP, ThumbScrollerUp)
			stage.addEventListener(MouseEvent.MOUSE_MOVE, ThumbScrollerMove)
					
			_xOffset = mouseX - trackerBar.x
		}

		private function ThumbScrollerUp(e:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, ThumbScrollerMove)
			stage.removeEventListener(MouseEvent.MOUSE_UP, ThumbScrollerUp)
		}

		private function ThumbScrollerMove(e:MouseEvent):void
		{
			trackerBar.x = checkPosition(mouseX - _xOffset)
			upDateThumbNailPosition()
		}

		public function ThumbScrollerWheel(e:MouseEvent):void
		{
			trackerBar.x -= e.delta * 9
			trackerBar.x = checkPosition(trackerBar.x)
			upDateThumbNailPosition()
		}

		private function TrackDown (e:MouseEvent):void
		{
			_xOffset = _barWidthRatio / 2
			trackerBar.x =  checkPosition(mouseX - _xOffset + 22)
			upDateThumbNailPosition()
		}
		
		private function upDateThumbNailPosition():void
		{
			_ratio = trackerBar.x / (trackerBackground.width - trackerBar.width)
			_scrollTo = -(_ratio * (_thumbContent.width - stage.stageWidth + 50))
			TweenLite.to(_thumbContent, _timeScroll, {x:_scrollTo, ease:Quint.easeOut})
		}
		
		private function checkPosition(position:Number):Number
		{
			position = Math.min(position,_xMax)
			position = Math.max(position, _xMin)
			return position
		}
	}
}